package com.yihu.wlyy.service.app.consult;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.yihu.wlyy.entity.consult.Consult;
import com.yihu.wlyy.entity.consult.ConsultPublic;
import com.yihu.wlyy.entity.consult.ConsultPublicReply;
import com.yihu.wlyy.entity.doctor.Doctor;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.repository.DiseaseDao;

/**
 * 图文咨询/公共咨询/病友圈业务处理类
 * @author George
 *
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class ConsultPublicService extends ConsultService {

	@Autowired
	private DiseaseDao diseaseDao;

	/**
	 * 根据咨询标识查询咨询信息
	 * @param consult
	 * @return
	 */
	public ConsultPublic findByConsult(String consult) {
		return consultPublicDao.findByConsult(consult);
	}

	/**
	 * 添加公共咨询
	 * @param patient 患者标识
	 * @return
	 */
	public ConsultPublic addConsult(ConsultPublic cp) {
		if (StringUtils.isNotEmpty(cp.getDisease()) && StringUtils.isEmpty(cp.getDiseaseName())) {
			cp.setDiseaseName(diseaseDao.findNameByCode(cp.getDisease()));
		}
		// 查询患者信息
		Patient patient = patientDao.findByCode(cp.getPatient());
		// 设置患者姓名
		cp.setName(patient.getName());
		// 设置患者头像
		cp.setPhoto(patient.getPhoto());
		// 保存咨询记录
		Consult consult = addConsult(patient.getCode(), cp.getTitle(), cp.getContent(), cp.getImages(), 4);
		cp.setConsult(consult.getCode());
		cp.setCzrq(new Date());
		cp.setDel("1");
		// 保存到数据库
		return consultPublicDao.save(cp);
	}

	/**
	 * 查询指定患者的公共咨询记录
	 * @param patient
	 * @return
	 */
	public Page<ConsultPublic> findPublicQuestionByPatient(String patient, String disease, long id, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		// 指定患者
		filters.put("patient", new SearchFilter("patient", Operator.EQ, patient));
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		// 按疾病类型过滤
		if (StringUtils.isNotEmpty(disease)) {
			filters.put("disease", new SearchFilter("disease", Operator.EQ, disease));
		}
		// 未作废
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
		Specification<ConsultPublic> spec = DynamicSpecifications.bySearchFilter(filters.values(), ConsultPublic.class);
		return consultPublicDao.findAll(spec, pageRequest);
	}

	/**
	 * 查询与医生相关的公共咨询
	 * @param doctor 医生标识
	 * @param type type 查询类型：0全部，1我的回答
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<ConsultPublic> findPublicQuestionByDoctor(String doctor, String disease, int type, long id, int pagesize) {
		if (id < 0) {
			id = 0;
		}
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		if (type == 0) {
			// 查询所有的公共咨询列表
			// 设置查询条件
			Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
			// 按疾病类型过滤
			if (StringUtils.isNotEmpty(disease)) {
				filters.put("disease", new SearchFilter("disease", Operator.EQ, disease));
			}else{
				//没有按疾病类型过滤的话就只查询高血压,糖尿病,和全部社区
				filters.put("disease", new SearchFilter("disease", Operator.LTE, "2"));
			}
			if (id > 0) {
				filters.put("id", new SearchFilter("id", Operator.LT, id));
			}
			// 未作废
			filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
			Specification<ConsultPublic> spec = DynamicSpecifications.bySearchFilter(filters.values(), ConsultPublic.class);
			return consultPublicDao.findAll(spec, pageRequest);
		} else {
			// 查询医生回答过的公共咨询列表
			if (id > 0) {
				return consultPublicDao.findRepliedPublicQuestionByDoctor(doctor, id, pageRequest);
			} else {
				return consultPublicDao.findRepliedPublicQuestionByDoctor(doctor, pageRequest);
			}
		}
	}

	/**
	 * 查询公共咨询的回复列表
	 * @param consult 咨询标识
	 * @param page 页码
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<ConsultPublicReply> findReplyListByConsult(String consult, long id, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		// 指定患者
		filters.put("consult", new SearchFilter("consult", Operator.EQ, consult));
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		// 未作废
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
		Specification<ConsultPublicReply> spec = DynamicSpecifications.bySearchFilter(filters.values(), ConsultPublicReply.class);
		return consultPublicReplyDao.findAll(spec, pageRequest);
	}

	/**
	 * 医生回复公共咨询
	 * @param reply
	 * @param imageCodes
	 * @return
	 */
	public ConsultPublicReply addDoctorReply(ConsultPublicReply reply) {
		// 查询医生信息
		Doctor doctor = doctorDao.findByCode(reply.getDoctor());
		// 设置医生姓名
		reply.setName(doctor.getName());
		// 设置医生头像
		reply.setPhoto(doctor.getPhoto());
		// 保存到数据
		ConsultPublicReply temp = consultPublicReplyDao.save(reply);
		if (temp != null) {
			// 回复数+1
			consultPublicDao.increaseAmount(reply.getConsult());
			// 患者未读数量+1
			consultPublicDao.increaseUnread(reply.getConsult());
		}
		return temp;
	}

	/**
	 * 患者回复公共咨询
	 * @param reply
	 * @return
	 */
	public ConsultPublicReply addPatientReply(ConsultPublicReply reply) {
		// 查询医生信息
		Patient patient = patientDao.findByCode(reply.getPatient());
		// 设置患者姓名
		reply.setName(patient.getName());
		// 设置患者头像
		reply.setPhoto(patient.getPhoto());
		// 保存到数据
		ConsultPublicReply temp = consultPublicReplyDao.save(reply);
		if (temp != null) {
			// 回复数+1
			consultPublicDao.increaseAmount(reply.getConsult());
		}
		return temp;
	}

	/**
	 * 清空未读数量
	 * @param consult
	 */
	public void clearUnread(String consult) {
		consultPublicDao.clearUnread(consult);
	}

}
